<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>产品管理</el-breadcrumb-item>
      <el-breadcrumb-item>产品详情</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <template>
        <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
          <el-tab-pane label="产品信息" name="product_info">
            <template>
              <!-- :rules="rules" -->
              <el-form :model="productForm" ref="productForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="产品名称" required style="width: 600px;" prop="name">
                  <el-input v-model="productForm.name"></el-input>
                </el-form-item>
                <el-form-item label="产品类型" required prop="category">
                  <el-select v-model="productForm.category" clearable placeholder="请选择分类">
                    <el-option v-for="item in productDictData.category" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="图片" required prop="thumbnail">
                  <el-upload class="upload-demo" accept="image/*" :headers="uploadParams.uploadHeaders" :data="uploadParams.uploadData"
                    :action="uploadParams.uploadFileUrl" :on-success="uploadSuccess">
                    <el-button size="small" type="primary">点击上传</el-button>
                  </el-upload>
                </el-form-item>
                <div v-if="productForm.thumbnail_path != ''" style="height: 150px;width: 300px;margin-left: 100px;margin-bottom: 20px;">
                  <el-image style="height: 100%;" :src="productForm.thumbnail_path" fit="fill"></el-image>
                </div>
                <el-form-item label="原价" required prop="old_price" style="width: 600px;">
                  <el-input type="number" v-model.number="productForm.old_price"></el-input>
                </el-form-item>
                <el-form-item label="现价" required prop="new_price" style="width: 600px;">
                  <el-input type="number" v-model.number="productForm.new_price"></el-input>
                </el-form-item>
                <el-form-item label="标签" prop="tags" style="width: 600px;">
                  <el-input v-model="productForm.tags"></el-input>
                </el-form-item>
                <el-form-item label="是否上架" prop="status" style="width: 600px;">
                  <el-select v-model="productForm.status" clearable placeholder="请选择">
                    <el-option v-for="item in productDictData.status" :key="item.value" :label="item.name" :value="item.value">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="显示">
                  <el-checkbox v-model="productForm.is_hot">热门</el-checkbox>
                  <el-checkbox v-model="productForm.is_recommend">置顶</el-checkbox>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="updateOrCreateProductForm">保存</el-button>
                  <el-button v-if="operate == 'add'" @click="resetForm">重置</el-button>
                </el-form-item>
              </el-form>
            </template>
          </el-tab-pane>
          <el-tab-pane label="产品详情" name="product_details">
            <el-button type="primary" @click="updateOrCreateProductForm" style="margin-bottom: 10px;">保存</el-button>
            <!-- <quill-editor v-model="productForm.details"></quill-editor> -->
            <el-upload class="upload-demo" :action="uploadParams.uploadFileUrl" :data="uploadParams.uploadData"
              :headers="uploadParams.uploadHeaders" :before-upload="uploadCheck" :on-success="productDetaiAddImgUploadSuccess">
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
            <div style="margin-top: 10px;margin-bottom: 100px;">
              <div style="display: flex;flex-wrap:wrap ;">
                <div v-for="image in productForm.details_images" :key="image.id" style="width: 300px;height: 150px;border: 1px solid #aaaa7f;margin: 10px;">
                  <el-image style="height: 100%;" :src="image.file_path" fit="fill"></el-image>
                  <div style="word-break:break-all">{{image.file_path}}</div>
                </div>
              </div>
            </div>
            <el-input type="textarea" :autosize="{ minRows: 2}" placeholder="请输入内容" v-model="productForm.details">
            </el-input>
          </el-tab-pane>
          <el-tab-pane label="产品图片" name="product_images">
            <el-upload class="upload-demo" :action="uploadParams.uploadFileUrl" :data="uploadParams.uploadData"
              :headers="uploadParams.uploadHeaders" :before-upload="uploadCheck" :on-success="productAddImgUploadSuccess">
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
            <div style="margin-top: 10px;">
              <div style="display: flex;flex-wrap:wrap ;">
                <div v-for="image in productForm.images" :key="image.id" style="width: 300px;height: 150px;border: 1px solid #aaaa7f;margin: 10px;">
                  <el-image style="height: 100%;" :src="image.file_path" fit="fill"></el-image>
                </div>
              </div>
            </div>
          </el-tab-pane>
          <el-tab-pane label="产品参数" name="product_params">
            <div style="margin-left: 50px;">
              <el-form :inline="true" :model="productParameterForm" class="demo-form-inline">
                <el-form-item label="产品参数">
                  <el-input v-model="productParameterForm.parameter" placeholder="产品参数"></el-input>
                </el-form-item>
                <el-form-item label="参数值">
                  <el-input v-model="productParameterForm.value" placeholder="参数值"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="addProductParameter">添加</el-button>
                </el-form-item>
              </el-form>
              <template>
                <el-table :data="productForm.parameters" style="width: 60%">
                  <el-table-column prop="parameter" label="产品参数" width="180">
                  </el-table-column>
                  <el-table-column prop="value" label="参数值" width="180">
                  </el-table-column>
                  <el-table-column fixed="right" label="操作" width="100">
                    <template slot-scope="scope">
                      <!-- <el-button type="text" size="small">查看</el-button> -->
                      <el-button type="text" @click="delProductParameter(scope.$index,scope.row.id)" size="small">删除</el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </template>
            </div>
          </el-tab-pane>
        </el-tabs>
      </template>
    </el-card>
  </div>
</template>

<script>
  import {
    getProductDictApi,
    updateOrCreateProductApi,
    getProductApi,
    addProductImageApi,
    _addProductDetailImageApi,
    _addProductParameterApi,
    _delProductParameterApi,
    getUploadParamsApi
  } from '../request/api.js'
  export default {
    data() {
      return {
        operate: 'add',
        productDictData: {}, // 产品分类
        productForm: {
          id: -1,
          name: '',
          category: null,
          new_price: '',
          old_price: '',
          thumbnail: 26,
          status: 1,
          tags: '',
          is_hot: false,
          is_recommend: false,
          details: '',
          thumbnail_path: '',
          parameters: []
        },
        productParameterForm: {}, // 产品参数
        // 文件数据
        uploadParams: {},
        // 页面样式
        activeName: 'product_info'
      }
    },
    created() {
      this.getProductDict()
      // 获取文件上传的参数配置
      this.uploadParams = getUploadParamsApi('product')
      // 判断是create or update
      const productId = this.$route.query.productId
      if (productId !== 'add') {
        this.operate = 'edit'
        this.getProduct(productId)
      }
    },
    methods: {
      // 获取分类数据
      async getProductDict() {
        const {
          data: res
        } = await getProductDictApi()
        console.log(res)
        this.productDictData = res.data
      },
      // 获取产品信息
      async getProduct(productId) {
        const {
          data: res
        } = await getProductApi(productId)
        console.log(res.data)
        this.productForm = res.data
      },
      // 创建更新
      updateOrCreateProductForm() {
        this.$refs.productForm.validate(async valid => {
          if (valid) {
            const {
              data: res
            } = await updateOrCreateProductApi(this.productForm, this.operate)
            console.log(res)
            if (res.status) {
              var message = this.operate === 'add' ? '添加成功！' : '编辑成功！'
              this.$message.success(message)
              if (this.operate === 'add') {
                this.operate = 'edit'
                this.productForm.id = res.data
              }
            } else {
              this.$message.error('操作失败！')
            }
          }
        })
      },
      // 重置表单
      resetForm() {
        this.$refs.productForm.resetFields()
      },
      // 文件上传
      uploadSuccess(response) {
        if (response.status) {
          // 保存id 和显示的图片地址
          this.productForm.thumbnail = response.data.id
          this.productForm.thumbnail_path = response.data.file_path
        }
      },
      // 产品图片
      uploadCheck() {
        if (this.productForm.id < 0) {
          this.$message.error('请先保存产品信息,在上传图片')
          return false
        }
        return true
      },
      async productAddImgUploadSuccess(response) {
        console.log(response)
        const {
          data: res
        } = await addProductImageApi(this.productForm.id, response.data.id)
        if (res.status) {
          this.getProduct(this.productForm.id)
          this.$message.success('添加成功')
        }
      },
      // 产品详情图片
      async productDetaiAddImgUploadSuccess(response) {
        console.log(response)
        const {
          data: res
        } = await _addProductDetailImageApi(this.productForm.id, response.data.id)
        if (res.status) {
          // this.getProduct(this.productForm.id)
          this.$message.success('添加成功')
        }
      },
      // 添加产品参数
      async addProductParameter() {
        const {
          data: res
        } = await _addProductParameterApi(this.productForm.id, this.productParameterForm)
        if (res.status) {
          console.log(res)
          this.$message.success('添加成功')
          var productParameterFormTemp = JSON.parse(JSON.stringify(this.productParameterForm))
          productParameterFormTemp.id = res.data
          this.productForm.parameters.splice(this.productForm.parameters.length, 0, productParameterFormTemp)
        }
      },
      // 删除产品参数
      async delProductParameter(index, parameterId) {
        const {
          data: res
        } = await _delProductParameterApi(this.productForm.id, parameterId)
        if (res.status) {
          this.productForm.parameters.splice(index, 1)
        }
      },
      // 标签页事件
      handleClick(tab, event) {
        this.activeName = tab.name
      }
    }
  }
</script>

<style lang="less" scoped>
</style>
